/**
 * 删除用户信息
 */
import { Button, Modal,message} from 'antd';
import React, { useState } from 'react';
import storage from '../../Utils/storage'
import $http from '../../Utils/request'


//创建请求头文件
let config = {}
if(storage.get("token") !== null){
  config = {
    headers:
      {'Content-Type': 'application/json',
        'token':storage.get("token").data
      }
  }
}else{
  config = {
    headers:
      {'Content-Type': 'application/json'
      }
  }
}
const DelUser = (props) => {
  const [isModalOpen, setIsModalOpen] = useState(false);
  const showModal = () => {


    //判断 删除的内容是否为1 ， 暂定只能删除一条，后续在进行批量删除
    if(props.getUser.length > 1 ){
      message.warning('只能选一项');
      return false;
    }
    setIsModalOpen(true);
  };
  const handleOk = () => {
    //进行后台请求
    $http.get("http://localhost/mall-basic-service/basic/user/deleteUserReact?userid="+props.getUser[0].userid, config).then((response)=>{
      //判断如果成功跳转到登录界面
      const code = response.data.code
      if(code === 200){
        setIsModalOpen(false);
        window.location.reload()
      }
    })
  };
  const handleCancel = () => {
    setIsModalOpen(false);
  };
  return (
    <div className={'delButDiv'}>
      <Button onClick={showModal}>
        删除用户
      </Button>
      <Modal title="删除用户" open={isModalOpen} onOk={handleOk} onCancel={handleCancel} >
          <span>
            确认要删除吗？
          </span>
      </Modal>
    </div>
  )
};
export default DelUser;